﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for GridColumnEditWindow.xaml
    /// </summary>
    public partial class GridColumnEditWindow : Window
    {
        public List<GridColumn> RetGridColumnList { get; private set; }
        private ObservableCollection<GridColumnModel> GridColumnList;
        public GridColumnEditWindow(List<GridColumn> gridColumnList)
        {
            InitializeComponent();
            GridColumnList = new ObservableCollection<GridColumnModel>();
            GridColumnModel root = new GridColumnModel();
            root.Name = "表格";
            root.Columns = new ObservableCollection<GridColumnModel>();
            root.IsSelected = true;
            root.IsExpanded = true;
            GridColumnList.Add((GridColumnModel)root);
            if (gridColumnList != null)
            {
                foreach(var item in gridColumnList)
                {
                    GridColumnModel target = (GridColumnModel)item;
                    target.Parent = root;
                    root.Columns.Add(target);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ctlHcombobox.Items.Clear();
            _ctlHcombobox.Items.Add("Left");
            _ctlHcombobox.Items.Add("Center");
            _ctlHcombobox.Items.Add("Right");

            _ctlVcombobox.Items.Clear();
            _ctlVcombobox.Items.Add("Top");
            _ctlVcombobox.Items.Add("Center");
            _ctlVcombobox.Items.Add("Bottom");

            _ctlGridColumnList.ItemsSource = GridColumnList;


        }

        private void Pre_Click(object sender, RoutedEventArgs e)
        {
            GridColumnConditionImageDetail window = new GridColumnConditionImageDetail((_ctlGridColumnList.SelectedItem as GridColumnModel).ImagePrefixs);
            if (window.ShowDialog() == true)
            {
                (_ctlGridColumnList.SelectedItem as GridColumnModel).ImagePrefixs = window.RetConditionImageList;
            }
        }

        private void Suf_Click(object sender, RoutedEventArgs e)
        {
            GridColumnConditionImageDetail window = new GridColumnConditionImageDetail((_ctlGridColumnList.SelectedItem as GridColumnModel).ImageSuffixs);
            if (window.ShowDialog() == true)
            {
                (_ctlGridColumnList.SelectedItem as GridColumnModel).ImageSuffixs = window.RetConditionImageList;
            }
        }
        
        private void _ctlGridColumnList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (_ctlGridColumnList.SelectedItem != null)
            {
                GridColumnModel selected = _ctlGridColumnList.SelectedItem as GridColumnModel;
                if (selected.Parent == null)
                {
                    _ctlGridColumnDetail.IsEnabled = false;
                    BindingOperations.ClearBinding(_ctlGridColumnDetail, Grid.DataContextProperty);
                }
                else
                {
                    _ctlGridColumnDetail.IsEnabled = true;

                    Binding binding = new Binding();
                    binding.Source = selected;
                    _ctlGridColumnDetail.SetBinding(Grid.DataContextProperty, binding);
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            GridColumnModel grid = ValidateName(GridColumnList[0]);
            if (grid != null)
            {
                grid.IsSelected = true;
                CustomMessageBox.ShowWarning("请填写名称等信息");
                return;
            }
            RetGridColumnList = new List<GridColumn>();
            foreach (var item in GridColumnList[0].Columns)
            {
                RetGridColumnList.Add((GridColumn)item);
            }
            DialogResult = true;
        }

        private GridColumnModel ValidateName(GridColumnModel gridColumnModel)
        {
            if (string.IsNullOrWhiteSpace(gridColumnModel.Name))
            {
                return gridColumnModel;
            }
            GridColumnModel ret;
            foreach (var item in gridColumnModel.Columns)
            {
                ret = ValidateName(item);
                if(ret != null)
                {
                    return ret;
                }
            }
            return null;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlGridColumnList.SelectedItem != null)
            {
                GridColumnModel selected = _ctlGridColumnList.SelectedItem as GridColumnModel;
                GridColumnModel newColumn = new GridColumnModel();
                newColumn.Name = "NewColumn";
                newColumn.Parent = selected;
                newColumn.Columns = new ObservableCollection<GridColumnModel>();
                selected.Columns.Add(newColumn);
                selected.IsExpanded = true;
            }
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlGridColumnList.SelectedItem != null)
            {
                GridColumnModel selected = _ctlGridColumnList.SelectedItem as GridColumnModel;
                if (selected.Parent != null)
                {
                    (_ctlGridColumnList.SelectedItem as GridColumnModel).Parent.Columns.Remove(_ctlGridColumnList.SelectedItem as GridColumnModel);
                }
            }
        }

        private void _ctlUp_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlGridColumnList.SelectedItem != null)
            {
                GridColumnModel selected = _ctlGridColumnList.SelectedItem as GridColumnModel;

                if (selected.Parent != null)
                {
                    GridColumnModel parent = selected.Parent;
                    int index = parent.Columns.IndexOf(selected);
                    if (index > 0)
                    {
                        parent.Columns.Remove(selected);
                        parent.Columns.Insert(index - 1, selected);
                    }
                }
            }
        }

        private void _ctlDown_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlGridColumnList.SelectedItem != null)
            {
                GridColumnModel selected = _ctlGridColumnList.SelectedItem as GridColumnModel;

                if (selected.Parent != null)
                {
                    GridColumnModel parent = selected.Parent;
                    int index = parent.Columns.IndexOf(selected);
                    if (index < parent.Columns.Count - 1)
                    {
                        parent.Columns.Remove(selected);
                        parent.Columns.Insert(index + 1, selected);
                    }
                }
            }
        }
    }
}
